<?php
/**
 * This object returns a section of Config as an object.
 * Some other objects recognise this and get their config
 * info from it.
 */
class Config_Section {
    /**
     * An array of config parameters.
     * @var array
     */
    protected $params = array();
    /**
     * Specifies whether this object is read only or not.
     * @var bool
     */
    protected $readonly;

    /**
     * The object constructor
     * @param array $config
     * @param bool readonly
     */
    public function __construct(&$config = array(), $readonly = false) {
        /*foreach($config as $key => $value) {
            $this->params[$key] = $value;
        }*/
        $this->params = &$config;
        $this->readonly = $readonly;
    }

    /**
     * The magic set method to set this objects properties.
     * A check is made so only existing properties for read-write objects can be changed.
     * @param string $key
     * @param mixed $value
     * @throws Config_Exception
     */
    public function __set($key, $value) {
        if($this->readonly) {
            throw new Config_Exception('Config is read only.');
        }
        if(array_key_exists($key, $this->params)) {
            $this->params[$key] = $value;
        } else {
            throw new Config_Exception("'$key' is not in config.");
        }
    }

    /**
     * The magic get method to get this objects properties.
     * Only existing properties can be read. An exception is thrown if the property doesn't exist.
     * @param string $key
     * @throws Config_Exception
     */
    public function __get($key) {
        if(array_key_exists($key, $this->params)) {
            return $this->params[$key];
        } else {
            throw new Config_Exception("'$key' is not in config.");
        }
    }
}
?>